package com.base.controller.utils.CZB;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Maps;
import com.base.helper.WebHelper;
import com.base.po.czb_sdk.CZBUtil;

import java.util.Date;
import java.util.Map;

public class CZBTokenUtils {
    private static String app_key = "appm_api_h598638374";
    private static String app_secret = "c42bde795fe5e6c4992a691b94f59f31";
    private static String platformType = "98638374";
//    private static final String app_key = "suibiandah51.0";
//    private static final String app_secret = "12ba273f41c9ac54e7303c0af5d80136";
//    private static final String platformType = "92656163";

    private static final String app_key_api = "suibianda1.0";
    private static final String app_secret_api = "16bc2edcaed84b3eaab079a16a4eeb54";
    private static final String platformType_api = "92654416";
    private static final String PAY_CODE_API = "1054";

    /**
     * 正式环境
     */
    private static final String FORMAL_CONTEXT = "https://mcs.czb365.com/services/v3/";
    /**
     * 测试环境
     */
    private static final String TEST_CONTEXT = "https://test-mcs.czb365.com/services/v3/";
    /**
     * 使用环境
     */
    private static final String USE_CONTEXT = FORMAL_CONTEXT;

    private static final String CZB_TOKEN_LOGIN = USE_CONTEXT + "begin/platformLoginSimpleAppV4";// 平台授权登录
    private static final String CZB_GAS_INFO = USE_CONTEXT + "gas/queryGasInfoListOilNoNew";// 初始化油站数据
    private static final String CZB_GAS_PRICE = USE_CONTEXT + "gas/queryPriceByPhone";// 根据用户查询油站状态和油价
    private static final String CZB_GEMERATE_ORDER = USE_CONTEXT + "orderws/generateOrder";//  订单推送接口
    private static final String CZB_CANCEL_ORDER = USE_CONTEXT + "orderws/cancelOrder";// 订单取消接口
    private static final String CZB_PAY_CALLBACK = USE_CONTEXT + "payCallbackApi";//  支付回告接口
    private static final String CZB_PAY_REFUND = USE_CONTEXT + "orderws/applyRefund";//  申请退款接口
    private static final String CZB_PAY_H5 = USE_CONTEXT + "orderws/applyRefund";//  申请退款接口
    private static final String CZB_PAY_SECRETCODE = USE_CONTEXT + "begin/getSecretCode"; //获取授权码接口

    public static void main(String[] args) {
        System.out.println("接口返回:"+getNowToken("18557542013"));
    }
    public static String getCzbPaySecretcode(String phone){
        Long Date = new Date().getTime();
        Map<String, Object> map = Maps.newHashMap();
        map.put("platformId", platformType);
        map.put("phone", phone);
        map.put("app_key", app_key);
        map.put("timestamp", Date);

        String sign = CZBUtil.getSign(map, app_secret);
        String result = WebHelper.PostData(CZB_PAY_SECRETCODE, "app_key=" + app_key + "&platformId=" + platformType + "&phone=" + phone + "&timestamp=" + Date + "&sign=" + sign, null, null);
        return result;
    }

    public static String getNowToken(String mobile) {
        Long Date = new Date().getTime();
        String result =CZBTokenUtils.createToken(Date, mobile);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Object ob = jsonObject.getString("result");
        JSONObject j = JSONObject.parseObject(ob.toString());
        String token = j.getString("token");
        return token;
    }

    public static String getNowToken1(String mobile, Long Date) {
        String result = CZBTokenUtils.createToken(Date, mobile);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Object ob = jsonObject.getString("result");
        JSONObject j = JSONObject.parseObject(ob.toString());
        String token = j.getString("token");
        return token;
    }

    public static String getNowTokenApi(String mobile) {
        Long Date = new Date().getTime();
        String result = CZBPayUtils.createTokenAPI(Date, mobile);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Object ob = jsonObject.getString("result");
        JSONObject j = JSONObject.parseObject(ob.toString());
        String token = j.getString("token");
        return token;
    }

    public static String createToken(Long DateTime, String mobile) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("platformType", platformType);
        map.put("platformCode", mobile);
        map.put("timestamp", DateTime);
        map.put("app_key", app_key);

        String sign = CZBUtil.getSign(map, app_secret);
        String result = WebHelper.PostData(CZB_TOKEN_LOGIN, "app_key=" + app_key + "&platformCode=" + mobile + "&platformType=" + platformType + "&timestamp=" + DateTime + "&sign=" + sign, null, null);
        return result;
    }

    public static String getAllGasInfo(Long DateTime, String mobile, String token) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("channelId", platformType);
        map.put("timestamp", DateTime);
        map.put("token", token);
        map.put("app_key", app_key);
        String sign = CZBUtil.getSign(map, app_secret);
        String result = WebHelper.PostData(CZB_GAS_INFO, "app_key=" + app_key + "&channelId=" + platformType + "&timestamp=" + DateTime + "&token=" + token + "&sign=" + sign, null, null);
        return result;
    }

    public static String getAllGasInfoApi(Long DateTime, String mobile, String token) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("channelId", platformType_api);
        map.put("timestamp", DateTime);
        map.put("token", token);
        map.put("app_key", app_key_api);
        String sign = CZBUtil.getSign(map, app_secret_api);
        String result = WebHelper.PostData(CZB_GAS_INFO, "app_key=" + app_key_api + "&channelId=" + platformType_api + "&timestamp=" + DateTime + "&token=" + token + "&sign=" + sign, null, null);
        return result;
    }

    public static String getGasDetails(Long DateTime, String gasIds, String mobile, String token) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("gasIds", gasIds);
        map.put("platformType", platformType);
        map.put("phone", mobile);
        map.put("timestamp", DateTime);
        map.put("token", token);
        map.put("app_key", app_key);
        String sign = CZBUtil.getSign(map, app_secret);
        String result = WebHelper.PostData(CZB_GAS_PRICE, "app_key=" + app_key + "&gasIds=" + gasIds + "&phone=" + mobile + "&platformType=" + platformType + "&timestamp=" + DateTime + "&token=" + token + "&sign=" + sign, null, null);
        return result;
    }

    public static String getGasDetailsApi(Long DateTime, String gasIds, String mobile, String token) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("gasIds", gasIds);
        map.put("platformType", platformType_api);
        map.put("phone", mobile);
        map.put("timestamp", DateTime);
        map.put("token", token);
        map.put("app_key", app_key_api);
        String sign = CZBUtil.getSign(map, app_secret_api);
        String result = WebHelper.PostData(CZB_GAS_PRICE, "app_key=" + app_key_api + "&gasIds=" + gasIds + "&phone=" + mobile + "&platformType=" + platformType_api + "&timestamp=" + DateTime + "&token=" + token + "&sign=" + sign, null, null);
        return result;
    }

    public static JSONArray getGasDetailsReal(Long DateTime, String gasIds, String mobile, String token) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("gasIds", gasIds);
        map.put("platformType", platformType);
        map.put("phone", mobile);
        map.put("timestamp", DateTime);
        map.put("token", token);
        map.put("app_key", app_key);
        String sign = CZBUtil.getSign(map, app_secret);
        String result = WebHelper.PostData(CZB_GAS_PRICE, "app_key=" + app_key + "&gasIds=" + gasIds + "&phone=" + mobile + "&platformType=" + platformType + "&timestamp=" + DateTime + "&token=" + token + "&sign=" + sign, null, null);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONArray jsonArray = null;
        if (jsonObject.get("code").equals(200)) {
            Object o1 = jsonObject.get("result");
            jsonArray = JSONArray.parseArray(o1.toString());
        }
        return jsonArray;
    }
}
